//
//  HomeViewController.swift
//

import JXCategoryView

class HomeViewController: BaseViewController {

    private var controllers = [RecViewController]()
    private let titles = ["推荐", "唱儿歌", "看动画", "读绘本", "听故事"]
    
    private lazy var navLeftView: HomeNavView = {
        let view = HomeNavView()
        return view
    }()
    
    private lazy var titleView: JXCategoryNumberView = {
        let lineView = JXCategoryIndicatorLineView()
        lineView.indicatorColor = .titleColor
        lineView.indicatorHeight = 4
        let titleView = JXCategoryNumberView()
        titleView.defaultSelectedIndex = 0
        titleView.delegate = self
        titleView.indicators = [lineView]
        titleView.titles = titles
        titleView.titleColor = .titleColor
        titleView.titleSelectedColor = .titleColor
        titleView.titleFont = 14.boldFont
        titleView.titleSelectedFont = 18.boldFont
        titleView.isTitleColorGradientEnabled = true
        titleView.backgroundColor = .yellowColor
        titleView.isAverageCellSpacingEnabled = true
        titleView.cellSpacing = 0
        return titleView
    }()
    
    private lazy var listContainerView: JXCategoryListContainerView = {
        let listContainerView = JXCategoryListContainerView(type: .scrollView, delegate: self)
        listContainerView?.setDefaultSelectedIndex(0)
        return listContainerView!
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupNav()
        addSubviews()
        configVC()
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        showAudioIcon()
    }
    
    private func setupNav() {
        navigation.bar.setBackgroundImage("nav_bg_all_88".image, for: .default)
        navigation.item.leftBarButtonItem = UIBarButtonItem(customView: navLeftView)
    }

    private func addSubviews() {
        automaticallyAdjustsScrollViewInsets = false

        view.addSubview(titleView)
        view.addSubview(listContainerView)

        titleView.snp.makeConstraints { (make) in
            make.left.right.equalToSuperview()
            make.top.equalTo(topLayoutGuideBottom)
            make.height.equalTo(40.fit)
        }

        listContainerView.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.top.equalTo(titleView.snp.bottom)
            make.bottom.equalTo(safeAreaLayoutGuideBottom)
        }

        titleView.contentScrollView = listContainerView.scrollView
    }
    
    private func configVC() {
        let rec = RecViewController(from: .rec)
        let song = RecViewController(from: .song)
        let animation = RecViewController(from: .animation)
        let book = RecViewController(from: .book)
        let story = RecViewController(from: .story)
        controllers = [rec, song, animation, book, story]
    }

    private func showAudioIcon() {
        if !AudioPlayerViewController.share.playerItemModel.id.isEmpty {
            navigation.item.rightBarButtonItem = UIBarButtonItem(image: "icon_nav_audio".image, style: .plain, target: self, action: #selector(audioPlayerAction))
        } else {
            navigation.item.rightBarButtonItem = nil
        }
    }
    
    @objc private func audioPlayerAction() {
        push(AudioPlayerViewController.share)
    }
}

// MARK: JXCategoryViewDelegate
extension HomeViewController: JXCategoryViewDelegate {
    
    func categoryView(_ categoryView: JXCategoryBaseView!, didClickSelectedItemAt index: Int) {
        listContainerView.didClickSelectedItem(at: index)
    }
}

// MARK: JXCategoryListContainerViewDelegate
extension HomeViewController: JXCategoryListContainerViewDelegate {
    
    func number(ofListsInlistContainerView listContainerView: JXCategoryListContainerView!) -> Int {
        return titles.count
    }
    
    func listContainerView(_ listContainerView: JXCategoryListContainerView!, initListFor index: Int) -> JXCategoryListContentViewDelegate! {
        return controllers[index]
    }
}
